import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import * as path from 'node:path';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user/entities/user.entity';
import { Role } from './role/entities/role.entity';
import { Permission } from './permission/entities/permission.entity';
import { UserModule } from './user/user.module';
import { RoleModule } from './role/role.module';
import { PermissionModule } from './permission/permission.module';
import { EnumerateModule } from './enumerate/enumerate.module';
import { Enumerate } from './enumerate/entities/enumerate.entity';
import { RedisModule } from './redis/redis.module';
import { CacheModule } from '@nestjs/cache-manager';
import { AuthModule } from './auth/auth.module';
import { JwtService } from '@nestjs/jwt';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { RoleGuard } from './role/role.guard';
import { AuthGuard } from './auth/auth.guard';
import { RedisService } from './redis/redis.service';
import { createClient } from 'redis';
import { UserService } from './user/user.service';
import { PermissionService } from './permission/permission.service';
import { RoleService } from './role/role.service';
import { LoggingInterceptor } from './interceptors/logging.interceptor';
import { PermissionGuard } from './permission/permission.guard';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true, // 设置为全局配置模块，应用中所有地方都能使用
      envFilePath: path.resolve(
        __dirname,
        `../.env.${process.env.NODE_ENV || 'production'}`,
      ), // 动态加载环境文件
    }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3307,
      username: 'root',
      password: 'root',
      database: 'nest_blog',
      // entities: [User, Role, Permission, Enumerate], // 引入你的实体类
      entities: [User, Role, Permission, Enumerate], // 引入你的实体类
      synchronize: true, // 在开发阶段使用，生产环境不要开启
    }),
    CacheModule.register({
      host: 'localhost',
      port: 6379, // Redis 端口
      password: 'root',
      ttl: 600, // 缓存默认过期时间，单位为秒
      isGlobal: true,
    }),
    TypeOrmModule.forFeature([User, Role, Permission]),
    UserModule,
    RoleModule,
    PermissionModule,
    EnumerateModule,
    RedisModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    JwtService,
    PermissionService,
    UserService,
    RoleService,
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor, // 注册为全局拦截器
    },
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
    {
      provide: APP_GUARD,
      useClass: RoleGuard, // 注册 RolesGuard 作为全局守卫
    },
    // {
    //   provide: APP_GUARD,
    //   useClass: PermissionGuard, // 注册 PermissionGuard 作为全局守卫
    // },
  ],
})
export class AppModule {}
